/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bld.model.categoria;

import br.com.bld.util.facade.AbstractDao;
import br.com.bld.util.facade.EntityFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

/**
 *
 * @author Pedro
 */
class CategoriaDao extends AbstractDao {

    public CategoriaDao(Connection conn, EntityFactory entityFactory) {
        super(conn, entityFactory);
    }

    private CategoriaPK getPk(ResultSet rs) throws SQLException {

        int i = 0;

        CategoriaPK resposta = new CategoriaPK(rs.getLong(++i));

        return resposta;
    }

    private CategoriaEntity getEntity(ResultSet rs) throws SQLException {
        CategoriaEntity resposta = new CategoriaEntity();

        int i = 1;

        resposta.setDescCategoria(rs.getString(++i));

        return resposta;
    }

    public int insert(CategoriaEntity entity, CategoriaPK pk) throws SQLException {

        String sql = createInsert();

        PreparedStatement ps = null;

        try {
            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());
            ps.setString(++i, entity.getDescCategoria());

            return ps.executeUpdate();
        } finally {
            close(ps);
        }
    }

    public int update(CategoriaEntity entity, CategoriaPK pk) throws SQLException {

        String sql = createUpdate();

        PreparedStatement ps = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setString(++i, entity.getDescCategoria());

            ps.setLong(++i, pk.getId());

            return executeUpdate(ps);

        } finally {
            close(ps);
        }
    }

    public int delete(CategoriaPK pk) throws SQLException {
        String sql = createDelete();

        PreparedStatement ps = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());

            return executeUpdate(ps);

        } finally {
            close(ps);
        }
    }

    public HashMap<CategoriaPK, CategoriaEntity> selectAll() throws SQLException {

        HashMap<CategoriaPK, CategoriaEntity> resposta = new HashMap();
        String sql = createSelect();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);
            rs = getResultSet(ps);

            while (rs.next()) {
                resposta.put(getPk(rs), getEntity(rs));
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public CategoriaEntity selectByPk(CategoriaPK pk) throws SQLException {

        CategoriaEntity resposta = null;

        String sql = createSelectByPk();

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);

            int i = 0;

            ps.setLong(++i, pk.getId());

            rs = getResultSet(ps);

            if (rs.next()) {
                resposta = getEntity(rs);
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }

    public HashMap<CategoriaPK, CategoriaEntity> selectByPage(Integer numrPagina, Integer qtdePorPagina) throws SQLException {

        HashMap<CategoriaPK, CategoriaEntity> resposta = new HashMap();
        String sql = createSelectByPage(numrPagina, qtdePorPagina);

        PreparedStatement ps = null;
        ResultSet rs = null;

        try {

            ps = getPreparedStatement(sql);
            rs = getResultSet(ps);

            while (rs.next()) {
                resposta.put(getPk(rs), getEntity(rs));
            }

            return resposta;
        } finally {
            close(ps, rs);
        }
    }
}
